package com.duang.netty.demo.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyClient {
    public static void connect(String host,int post){
        NioEventLoopGroup gr = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try {
            ChannelFuture future = bootstrap.group(gr)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        protected void initChannel(SocketChannel sc) {
                            ChannelPipeline pl = sc.pipeline();
                            pl.addLast(new ClientHandler());
                        }
                    }).connect(host, post).sync();
            future.sync();
        }catch (Exception e) {
            e.printStackTrace();
            gr.shutdownGracefully();
        }

    }

    public static void main(String[] args) {
        connect("127.0.0.1",9999);
    }
}
